package com.ww.core.widget.edittext;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;

/**
 * @since 
 * @version 2013-6-28
 * @author wangwei
 */
public class SendEditText extends EditText {
	private Drawable dLeft;
	Context context;

	public SendEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initEditText();
	}

	public SendEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		initEditText();
	}

	public SendEditText(Context context) {
		super(context);
		initEditText();
	}

	@Override
	public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
		if (left != null) {
			dLeft = left;
		}

		super.setCompoundDrawables(left, top, right, bottom);
	}

	public Drawable getleftDrawable() {
		return dLeft;
	}

	@Override
	protected void finalize() throws Throwable {
		dLeft = null;
		super.finalize();
	}

	// 初始化edittext 控件
	private void initEditText() {
		setEditTextDrawable();
		addTextChangedListener(new TextWatcher() { // 对文本内容改变进行监听
			public void afterTextChanged(Editable paramEditable) {
			}

			public void beforeTextChanged(CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3) {
			}

			public void onTextChanged(CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3) {
				SendEditText.this.setEditTextDrawable();
			}
		});
	}

	// 控制图片的显示
	private void setEditTextDrawable() {
		if (getText().toString().length() == 0) {
			setCompoundDrawables(this.dLeft, null, null, null);
		} else {
			setCompoundDrawables(null, null, null, null);
		}
	}

}